package com.huaqin.obeacons;

import com.huaqin.obeacons.utils.SharedPreferenceUtil;
import com.umeng.fb.FeedbackAgent;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MoreFragment extends Fragment implements OnClickListener {

	private final String TAG = "LogOutActivity";
	private RelativeLayout updateLayout;
	private RelativeLayout feedbackLayout;

	private RelativeLayout soundSwitchLayout;
	private RelativeLayout vibratorSwitchLayout;
	private ImageView soundOpenView;
	private ImageView soundCloseView;
	private ImageView vibratorOpenView;
	private ImageView vibratorCloseView;
	private RelativeLayout aboutMeLayout;
	private RelativeLayout usrInfoLayout;
	private RelativeLayout logOutLayout;
	private RelativeLayout dataReportLayout;
	private RelativeLayout appInstructLayout;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_more, null);
		updateLayout = (RelativeLayout) view.findViewById(R.id.app_update);
		updateLayout.setOnClickListener(this);
		feedbackLayout = (RelativeLayout) view.findViewById(R.id.app_feedback);
		feedbackLayout.setOnClickListener(this);

		soundSwitchLayout = (RelativeLayout) view
				.findViewById(R.id.switch_sound);
		soundOpenView = (ImageView) view.findViewById(R.id.st_open_sound);
		soundCloseView = (ImageView) view.findViewById(R.id.st_close_sound);
		soundSwitchLayout.setOnClickListener(this);

		vibratorSwitchLayout = (RelativeLayout) view
				.findViewById(R.id.switch_vibrate);
		vibratorOpenView = (ImageView) view.findViewById(R.id.st_open_vibrate);
		vibratorCloseView = (ImageView) view
				.findViewById(R.id.st_close_vibrate);
		vibratorSwitchLayout.setOnClickListener(this);

		boolean isVoiceEnable = SharedPreferenceUtil
				.getAppVoiceEnable(getActivity());
		boolean isVibrateEnable = SharedPreferenceUtil
				.getAppVibrateEnable(getActivity());
		if (isVoiceEnable) {
			soundOpenView.setVisibility(View.VISIBLE);
			soundCloseView.setVisibility(View.INVISIBLE);
		} else {
			soundOpenView.setVisibility(View.INVISIBLE);
			soundCloseView.setVisibility(View.VISIBLE);
		}
		if (isVibrateEnable) {
			vibratorOpenView.setVisibility(View.VISIBLE);
			vibratorCloseView.setVisibility(View.INVISIBLE);
		} else {
			vibratorOpenView.setVisibility(View.INVISIBLE);
			vibratorCloseView.setVisibility(View.VISIBLE);
		}

		aboutMeLayout = (RelativeLayout) view.findViewById(R.id.app_aboutme);
		aboutMeLayout.setOnClickListener(this);

		usrInfoLayout = (RelativeLayout) view.findViewById(R.id.account_info);
		usrInfoLayout.setOnClickListener(this);
		logOutLayout = (RelativeLayout) view.findViewById(R.id.account_logout);
		logOutLayout.setOnClickListener(this);

		dataReportLayout = (RelativeLayout) view.findViewById(R.id.data_report);
		dataReportLayout.setOnClickListener(this);
		appInstructLayout = (RelativeLayout) view
				.findViewById(R.id.app_instruct);
		appInstructLayout.setOnClickListener(this);

		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.account_info:
			getActivity().startActivity(
					new Intent(getActivity(), UserInfoActivity.class));
			break;
		case R.id.account_logout:
			getActivity().startActivity(
					new Intent(getActivity(), LogOutActivity.class));
			break;
		case R.id.switch_sound:
			if (soundOpenView.getVisibility() == View.VISIBLE) {
				soundOpenView.setVisibility(View.INVISIBLE);
				soundCloseView.setVisibility(View.VISIBLE);
				SharedPreferenceUtil.setAppVoiceEnable(getActivity(), false);
			} else {
				soundOpenView.setVisibility(View.VISIBLE);
				soundCloseView.setVisibility(View.INVISIBLE);
				SharedPreferenceUtil.setAppVoiceEnable(getActivity(), true);
			}
			break;
		case R.id.switch_vibrate:
			if (vibratorOpenView.getVisibility() == View.VISIBLE) {
				vibratorOpenView.setVisibility(View.INVISIBLE);
				vibratorCloseView.setVisibility(View.VISIBLE);
				SharedPreferenceUtil.setAppVibrateEnable(getActivity(), false);
			} else {
				vibratorOpenView.setVisibility(View.VISIBLE);
				vibratorCloseView.setVisibility(View.INVISIBLE);
				SharedPreferenceUtil.setAppVibrateEnable(getActivity(), true);
			}
			break;
		case R.id.app_update:
			UmengUpdateAgent.forceUpdate(getActivity().getApplicationContext());
			UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {
				@Override
				public void onUpdateReturned(int arg0, UpdateResponse response) {
					// TODO Auto-generated method stub
					Log.d(TAG, "response.hasUpdate " + response.hasUpdate);
					if(!response.hasUpdate){
						Toast.makeText(getActivity(), "亲，当前已经是最新版本了",
								Toast.LENGTH_SHORT).show();
					}
				}
			});
			System.out
					.println("MoreFragment.onCreateView(updateLayout).new OnClickListener() {updateLayout}.onClick()");
			break;
		case R.id.app_feedback:
			FeedbackAgent agent = new FeedbackAgent(getActivity());
			agent.sync();
			agent.startFeedbackActivity();
			System.out
					.println("MoreFragment.onCreateView(feedbackLayout).new OnClickListener() {feedbackLayout}.onClick()");
			break;
		case R.id.app_aboutme:
			getActivity().startActivity(
					new Intent(getActivity(), AboutMeActivity.class));
			break;
		case R.id.data_report:
			Toast.makeText(getActivity(), "亲，该功能正在开发中，敬请期待。。。",
					Toast.LENGTH_SHORT).show();
			break;
		case R.id.app_instruct:
			getActivity().startActivity(
					new Intent(getActivity(), InstructActivity.class));
			break;
		default:
			break;
		}
	}
}